#include <stdio.h>
#include <stdlib.h>
#include <conio.h> 
#include <windows.h> 

#define	high 20					//宏定义设置游戏界面大小 
#define	width 30

void gotoxy(int x,int y)		//光标移动到（x，y）的位置 
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle,pos); 
} 

void HideCursor()				//隐藏光标 
{
	CONSOLE_CURSOR_INFO cursor_info={1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}

int ball_x,ball_y;			    //小球的位置，x竖着，y横着 
int ball_vx,ball_vy;		    //小球速度 
int board_x,board_y;			//木板中心位置 
int r;							//木板半径 
int left,right;					//木板左右位置 
int clay_x[high/4]={0},clay_y[width]={0};
								//砖块的位置 
int canvas[high][width]={0};	//二维数组储存游戏，3显示砖块，0为空格
								//2为木板 ，1为小球 
int score;						//消除砖块数 

void startup()					//数据初始化
{
	system("COLOR 9"); 		//设置颜色 
	system("COLOR 6");
	
	ball_x=high/2-3;
	ball_y=width/2;
	canvas[ball_x][ball_y]=1;
	
	ball_vx=ball_vy=1;
	
	//以下设置木板的位置 
	board_y=width/2;
	board_x=high-2;
	r=3;
	left=board_y-r;
	right=board_y+r;
	int k,i;
	
	for(k=left;k<=right;k++)
	canvas[board_x][k] = 2;	


	for(i=0;i<high/4;i++)		//初始化砖块的位置 
		for(k=0;k<width;k++)
			canvas[i][k] = 3;
		
	score=0;					//初始化消除砖块数 
	
	HideCursor();
} 
void show()						//显示界面 
{
	int i,j;
	
	gotoxy(0,0);				//光标移动到原点位置，以下重新清屏 
	
	for(i=0;i<=high;i++)
	{
		for(j=0;j<=width;j++)
		{
			if(canvas[i][j]==1)			//输出小球
			printf("o");		 
			else if(canvas[i][j] == 2)	//输出木板 
			printf("*");
			else if(canvas[i][j] == 0)	//输出空格 
			printf(" ");
			else if(canvas[i][j] == 3)	//输出砖块 
			printf("#");
		}
		printf("|\n");					//输出右边框和换行 
	}
	for (j=0;j<width;j++)				// 显示下边界
		printf("-"); 
	
	putchar('\n');
	printf("消除砖块数:%d\n",score);
}

void updatewith()							//与用户输出有关 
{
	char input;								//控制移动 
		
	if (kbhit())
	{
		input = getch();
		if(input == 'a' && left > 0)		//向左移动 
		{
			canvas[board_x][right]=0;
			board_y--;
			left  = board_y - r;
			right = board_y + r;
			canvas[board_x][left]=2; 
		} 
		if(input == 'd' && right < (width-1))//向右移动 
		{
			canvas[board_x][left]=0;
			board_y++;
			left  = board_y - r;
			right = board_y + r;
			canvas[board_x][right]=2;
		} 
	}
}

void updatewithout()			//与用户输出无关 
{
	static int speed=0;			//控制小球的速度
	if(speed < 8)
	speed++; 
	if(speed == 7)
	{
		speed = 0;
		
	// 更新小球坐标
		canvas[ball_x][ball_y] = 0;	
		ball_x = ball_x + ball_vx;
		ball_y = ball_y + ball_vy;		
		canvas[ball_x][ball_y] = 1;
	}
	
	//控制小球碰到边界后反弹
	if(ball_x == 0)				 
	ball_vx = -ball_vx;
	if((ball_y == width-1) || (ball_y == 0))
	ball_vy = -ball_vy;
	
	//控制小球碰到砖块后反弹
	if (canvas[ball_x-1][ball_y] == 3)
	{
		ball_vx = -ball_vx;
		canvas[ball_x-1][ball_y] = 0;
		putchar('\a');
		score++;
	}
	
	//控制小球碰到木板后反弹 
	if(ball_x == board_x-1)
	{
		if((ball_y >= (board_y-r)) && (ball_y <= (board_y+r)))
			ball_vx = -ball_vx;
	}
	else if(ball_x == board_x)
	{
		puts("'Game Over'");
		exit(0);
	}	
}
 
int main() 
{
	startup();					//数据初始化
	while(1)					//游戏循环执行 
	{
		show();					//显示界面 
		updatewith();			//与用户输出有关 
		updatewithout();		//与用户输出无关 
	} 
	return 0;
}